home *** CD-ROM | disk | FTP | other *** search
- /*
- * Listing 4 - An example of a stack abstraction with magic numbers
- */
-
- #include <stdlib.h>
- #include <assert.h>
- #include <stack.h>
-
- static char *magic = "Stack";
- #define MAGIC_ON(p) ((p)->stk_magic = magic)
- #define MAGIC_OFF(p) ((p)->stk_magic = NULL)
- #define MAGIC_CHECK(p) assert((p != NULL) && ((p)->stk_magic == magic))
-
- /* Create a stack */
- STK *StkConstruct() {
- STK *stk;
-
- stk = malloc(sizeof(STK));
- assert(stk != NULL);
- stk->stk_top = 0;
- MAGIC_ON(stk);
- MAGIC_CHECK(stk);
- return(stk);
- }
-
- /* Destroy a stack */
- void StkDestroy(STK *stk) {
- MAGIC_CHECK(stk);
- MAGIC_OFF(stk);
- free(stk);
- }
-
- /* Push an item on the stack */
- void StkPush(STK *stk, void *item) {
- MAGIC_CHECK(stk);
- assert(stk->stk_top < STACK_SIZE);
- stk->stk_stack[stk->stk_top++] = item;
- }
-
- /* Pop an item from the stack */
- void *StkPop(STK *stk) {
- MAGIC_CHECK(stk);
- assert(stk->stk_top > 0);
- return(stk->stk_stack[--(stk->stk_top)]);
- }
-
- /* Determine if the stack is empty */
- int StkIsEmpty(STK *stk) {
- MAGIC_CHECK(stk);
- return(stk->stk_top == 0);
- }
-
-